<?php

namespace app\common\model;

/**
 * 爆品礼物模型
 * Class Goods
 * @package app\common\model
 */
class Gifts extends BaseModel
{
    protected $name = 'gifts';

    /**
     * 获取商品列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function giftsList($data)
    {
        // 筛选条件
        $filter = [];
        $filter['is_delete'] = 0;
        $page = isset($param['page']) && is_numeric($param['page']) ? intval($param['page']) : 1;
        $limit = 10;
        $offset = ($page - 1) * $limit;
        // 执行查询
        $list = $this->with(['image'])
            ->where($filter)
            ->order(['sort', 'gifts_id' => 'desc'])
            ->limit($offset, $limit)
            ->select();
        // 整理列表数据并返回
        return $list;
    }

    /**
     * 关联商品图片表
     * @return \think\model\relation\HasMany
     */
    public function image()
    {
        return $this->hasOne('app\\common\\model\\UploadFile', 'file_id', 'image_id');
    }


    /**
     * 状态
     * @param $value
     * @return mixed
     */
    public function getGiftsStatusAttr($value)
    {
        $status = [10 => '上架', 20 => '下架'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 获取商品列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        // 商品列表获取条件
        $params = array_merge([
            'status' => 10,         // 商品状态
            'search' => '',         // 搜索关键词
            'sortType' => 'all',    // 排序类型
            'listRows' => 15,       // 每页数量
        ], $param);

        // 筛选条件
        $filter = [];
        $params['status'] > 0 && $filter['gifts_status'] = $params['status'];
        !empty($params['search']) && $filter['gifts_title'] = ['like', '%' . trim($params['search']) . '%'];
        // 排序规则
        $sort = [];
        if ($params['sortType'] === 'all') {
            $sort = ['sort', 'gifts_id' => 'desc'];
        }

        // 执行查询
        $list = $this->with(['image'])
            ->where('is_delete', '=', 0)
            ->where($filter)
            ->order($sort)
            ->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ]);

        // 整理列表数据并返回
        return $list;
    }


    /**
     * 有效期-开始时间
     * @param $value
     * @return mixed
     */
    public function getStartTimeAttr($value)
    {
        return ['text' => date('Y-m-d', $value), 'value' => $value];
    }

    /**
     * 有效期-结束时间
     * @param $value
     * @return mixed
     */
    public function getEndTimeAttr($value)
    {
        return ['text' => date('Y-m-d', $value), 'value' => $value];
    }



    /**
     * 获取商品详情
     * @param $giftsId
     * @return static
     */
    public static function detail($giftsId)
    {
        /* @var $model self */
        $model = (new static)->with([
            'image',
        ])->where(['gifts_id'=>$giftsId,'is_delete'=>0])
            ->find();

        if (empty($model)) {
            return $model;
        }
        // 整理商品数据并返回
        return $model;
    }



    /**
     * 获取商品详情
     * @param $giftsId
     * @return static
     */
    public static function apiDetail($giftsId)
    {
        /* @var $model self */
        $model = (new static)->with([
            'image',
        ])->where(['gifts_id'=>$giftsId,'is_delete'=>0,'gifts_status'=>10])
            ->find();

        if (empty($model)) {
            return $model;
        }
        // 整理商品数据并返回
        return $model;
    }




}
